package com.ittx.dzt.jinglyun.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.DecryptReceiverInfoDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.jinglyun.handler.AbstractBaseJinglYunApiHandler;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 订单解密
 * <p>
 * cy.order.decrypt
 * /open/api/batchDecrypt/plaintext
 * 加密信息批量解密 获取收货人明文信息，建议拉取频次：10次/秒
 * https://jlopen.jinglyun.com/main/doc?docType=2&cateId=10001&docId=20048
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/11/4
 */
@Slf4j
@Service
public class JinglYunOrderDecryptService extends AbstractBaseJinglYunApiHandler<DecryptReceiverInfoDTO> {


	@Override
	public String getJinglMethod() {
		return "/open/api/batchDecrypt/plaintext";
	}

	/**
	 * 本地api
	 *
	 * @return 本地api名称 解密
	 */
	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SALES_ORDER_DECRYPT;
	}

	/**
	 * 执行过程
	 *
	 * @param platformApiParamDTO    平台API参数
	 * @param decryptReceiverInfoDTO 解密收件人信息参数
	 * @return 返回数据
	 */
	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, DecryptReceiverInfoDTO decryptReceiverInfoDTO) {
		try {
			// 店铺配置
			StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
			Map<String, Object> params = new HashMap<>();
			decryptReceiverInfoDTO.setMobileIsOk(false);
			decryptReceiverInfoDTO.setPhoneIsOk(false);
			decryptReceiverInfoDTO.setAttentionToIsOk(false);
			decryptReceiverInfoDTO.setAddressIsOk(false);

			if (StrUtil.isNotEmpty(decryptReceiverInfoDTO.getShipToPhone()) && StrUtil.isNotEmpty(decryptReceiverInfoDTO.getDecryptShipToPhone())) {
				List<Map<String, String>> decryptionReqList = new ArrayList<>();
				Map<String, String> decryptMap = new HashMap<>();
				decryptMap.put("deSensitiveText", decryptReceiverInfoDTO.getShipToPhone());
				decryptMap.put("ciphertext", decryptReceiverInfoDTO.getDecryptShipToPhone());
				decryptionReqList.add(decryptMap);
				params.put("decryptionReqList", decryptionReqList);
				Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
				if (result.hasError()) {
					return result;
				}
				Map<String, Object> resultMap = (Map<String, Object>) result.getData();
				List itemList = MapUtil.get(resultMap, "entry", List.class);
				Map<String, Object> entryMap = Convert.toMap(String.class, Object.class, itemList.get(0));
				Boolean decryptSuccess = MapUtil.getBool(entryMap, "decryptSuccess");
				decryptReceiverInfoDTO.setPhoneIsOk(decryptSuccess);
				if (decryptSuccess) {
					decryptReceiverInfoDTO.setDecryptShipToPhone(MapUtil.getStr(entryMap, "primitivePhone"));
				} else {
					decryptReceiverInfoDTO.setPhoneErrorMsg(MapUtil.getStr(entryMap, "failMsg"));
				}
			}
			if (StrUtil.isNotEmpty(decryptReceiverInfoDTO.getShipToMobile()) && StrUtil.isNotEmpty(decryptReceiverInfoDTO.getDecryptShipToMobile())) {
				List<Map<String, String>> decryptionReqList = new ArrayList<>();
				Map<String, String> decryptMap = new HashMap<>();
				decryptMap.put("deSensitiveText", decryptReceiverInfoDTO.getShipToMobile());
				decryptMap.put("ciphertext", decryptReceiverInfoDTO.getDecryptShipToMobile());
				decryptionReqList.add(decryptMap);
				params.put("decryptionReqList", decryptionReqList);
				Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
				if (result.hasError()) {
					return result;
				}
				Map<String, Object> resultMap = (Map<String, Object>) result.getData();
				List itemList = MapUtil.get(resultMap, "entry", List.class);
				Map<String, Object> entryMap = Convert.toMap(String.class, Object.class, itemList.get(0));
				Boolean decryptSuccess = MapUtil.getBool(entryMap, "decryptSuccess");
				decryptReceiverInfoDTO.setMobileIsOk(decryptSuccess);
				if (decryptSuccess) {
					decryptReceiverInfoDTO.setDecryptShipToMobile(MapUtil.getStr(entryMap, "primitivePhone"));
				} else {
					decryptReceiverInfoDTO.setMobileErrorMsg(MapUtil.getStr(entryMap, "failMsg"));
				}
			}
			if (StrUtil.isNotEmpty(decryptReceiverInfoDTO.getShipToAttentionTo()) && StrUtil.isNotEmpty(decryptReceiverInfoDTO.getDecryptShipToAttentionTo())) {
				List<Map<String, String>> decryptionReqList = new ArrayList<>();
				Map<String, String> decryptMap = new HashMap<>();
				decryptMap.put("deSensitiveText", decryptReceiverInfoDTO.getShipToAttentionTo());
				decryptMap.put("ciphertext", decryptReceiverInfoDTO.getDecryptShipToAttentionTo());
				decryptionReqList.add(decryptMap);
				params.put("decryptionReqList", decryptionReqList);
				Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
				if (result.hasError()) {
					return result;
				}
				Map<String, Object> resultMap = (Map<String, Object>) result.getData();
				List itemList = MapUtil.get(resultMap, "entry", List.class);
				Map<String, Object> entryMap = Convert.toMap(String.class, Object.class, itemList.get(0));
				Boolean decryptSuccess = MapUtil.getBool(entryMap, "decryptSuccess");
				decryptReceiverInfoDTO.setAttentionToIsOk(decryptSuccess);
				if (decryptSuccess) {
					decryptReceiverInfoDTO.setDecryptShipToAttentionTo(MapUtil.getStr(entryMap, "primitivePhone"));
				} else {
					decryptReceiverInfoDTO.setAttentionToErrorMsg(MapUtil.getStr(entryMap, "failMsg"));
				}
			}
			if (StrUtil.isNotEmpty(decryptReceiverInfoDTO.getShipToAddress()) && StrUtil.isNotEmpty(decryptReceiverInfoDTO.getDecryptShipToAddress())) {
				List<Map<String, String>> decryptionReqList = new ArrayList<>();
				Map<String, String> decryptMap = new HashMap<>();
				decryptMap.put("deSensitiveText", decryptReceiverInfoDTO.getShipToAddress());
				decryptMap.put("ciphertext", decryptReceiverInfoDTO.getDecryptShipToAddress());
				decryptionReqList.add(decryptMap);
				params.put("decryptionReqList", decryptionReqList);
				Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
				if (result.hasError()) {
					return result;
				}
				Map<String, Object> resultMap = (Map<String, Object>) result.getData();
				List itemList = MapUtil.get(resultMap, "entry", List.class);
				Map<String, Object> entryMap = Convert.toMap(String.class, Object.class, itemList.get(0));
				Boolean decryptSuccess = MapUtil.getBool(entryMap, "decryptSuccess");
				decryptReceiverInfoDTO.setAddressIsOk(decryptSuccess);
				if (decryptSuccess) {
					decryptReceiverInfoDTO.setDecryptShipToAddress(MapUtil.getStr(entryMap, "primitivePhone"));
				} else {
					decryptReceiverInfoDTO.setAddressErrorMsg(MapUtil.getStr(entryMap, "failMsg"));
				}
			}
			return Result.success().addData(JacksonTool.toJson(decryptReceiverInfoDTO));
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			log.error(msg, e.fillInStackTrace());
			ExceptionLog.error(e, msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}

}
